package com.mindai.esper.monitor.biz.exception;

import com.mindai.esper.monitor.biz.enums.BizErrorCode;
import lombok.Data;

/**
 * Created by Ladd on 2016/11/11.
 */
@Data
public class BaseBizException extends RuntimeException {

    BizErrorCode codeEnum;

    String errorCode;

    String errorMsg;

    /**
     * 除了错误码本身描述的提示信息外，额外补充的信息
     */
    private String extraMsg;

    /**
     * 错误信息模板
     */
    private final static String MSG_TEMPLATE = "错误码:{0}, 描述:{1}, 异常信息:{2}";


    /**
     * 构造器
     */
    public BaseBizException() {
        super();
    }

    /**
     * 构造器，含异常信息
     *
     * @param e 支付业务异常
     */
    public BaseBizException(BaseBizException e) {
        super(e);
        this.codeEnum = e.getCodeEnum();
        this.errorCode = e.getCodeEnum().getCode();
        this.errorMsg = e.getCodeEnum().getMsg();
    }

    /**
     * 构造器，含错误明细码枚举
     *
     * @param codeEnum 错误码
     */
    public BaseBizException(BizErrorCode codeEnum) {
        super(codeEnum.getMsg());
        this.codeEnum = codeEnum;
        this.errorCode = codeEnum.getCode();
        this.errorMsg = codeEnum.getMsg();
    }

    /**
     * 构造器，含错误明细码枚举、自定义错误信息
     *
     * @param codeEnum 错误码
     * @param extraMsg 错误描述
     */
    public BaseBizException(BizErrorCode codeEnum, String extraMsg) {
        super(extraMsg == null ? codeEnum.getMsg() : codeEnum.getMsg() + "-" + extraMsg);
        this.codeEnum = codeEnum;
        this.errorCode = codeEnum.getCode();
        this.errorMsg = codeEnum.getMsg();
        this.extraMsg = extraMsg;
    }

    /**
     * 构造器，含错误明细码枚举、异常
     *
     * @param codeEnum 错误码
     * @param cause    异常
     */
    public BaseBizException(BizErrorCode codeEnum, Throwable cause) {
        super(codeEnum.getMsg(), cause);
        this.codeEnum = codeEnum;
        this.errorCode = codeEnum.getCode();
        this.errorMsg = codeEnum.getMsg();
    }

    public BaseBizException(BizErrorCode codeEnum, Throwable cause, String extraMsg) {
        super(codeEnum.getMsg() + "-" + extraMsg, cause);
        this.codeEnum = codeEnum;
        this.errorCode = codeEnum.getCode();
        this.errorMsg = codeEnum.getMsg();
        this.extraMsg = extraMsg;
    }

    /**
     * 组装错误信息
     *
     * @return 错误信息详细描述
     */
    public String getMessage() {
        return this.extraMsg == null ? this.errorMsg : this.errorMsg + "-" + this.extraMsg;
    }
}
